import {
  getListPage,
  createRow,
  updateRow,
  deleteRow,
  deleteMore
} from '@/api/table'
import {
  UploaderImage,
  Kindeditor
} from '@/layout/components'
export default {
  components: {
    UploaderImage,
    Kindeditor
  },
  filters: { // 字段转换
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      info: {
        title: ''
      },
      empty_text: '暂无数据',
      listParam: {
        currentPage: 1,
        pagesize: 5,
        total: 0
      },
      datas: null,
      listLoading: true,
      formDialog: {
        visible: false,
        action: '',
        title: '提示',
        width: '60%',
        center: true
      },
      formData: {
        systemId: 1,
        organizationId: 1,
        chargeId: 2,
        price: 0.00,
        coverPath: '',
        content: ''
      },
      formLabelWidth: '120px'
    }
  },
  created() {
    this._fetchData()
    if (this.$route.query.title) {
      this.info.title = this.$route.query.title
    } else {
      this.info.title = '可可西里培训学校'
    }
  },
  watch: {
    listParam: {
      handler() {
        this._fetchData()
      },
      deep: true // 深度监听，同时也可监听到param参数变化
    }
  },
  methods: {
    _fetchData() {
      this.listLoading = true
      getListPage(this.listParam).then(response => {
        this.datas = response.data.items
        this.listParam.total = response.data.total
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.listParam.pagesize = val
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.listParam.currentPage = val
    },
    _fromUploaderImage(data) {
      console.log(data)
      this.formData.coverPath = data
    },
    _fromKindeditor(data) {
      console.log(data)
      this.formData.content = data
    },
    _formDialogConfirm() {
      // console.log(this.formDialog)
      // console.log(this.formData)
      switch (this.formDialog.action) {
        case 'add':
          createRow(this.formData).then(response => {
            if (response.code === 200 || response.code === 20000) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
            } else {
              this.$message({
                type: 'info',
                message: '操作失败'
              })
            }
          })
          break
        case 'edit':
          updateRow(this.formData).then(response => {
            if (response.code === 200 || response.code === 20000) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
            } else {
              this.$message({
                type: 'info',
                message: '操作失败'
              })
            }
          })
          break
        default:
          break
      }
    },
    createRow(row) {
      this.formDialog.visible = true
      this.formDialog.title = '新增'
      this.formDialog.action = 'add'
    },
    updateRow(row) {
      this.formDialog.visible = true
      this.formDialog.title = '编辑'
      this.formDialog.action = 'edit'
      console.log(this.formData)
      console.log(row)
      this.formData = row
    },
    detailRow(row) {
      this.formDialog.visible = true
      this.formDialog.title = '查看'
      this.formDialog.action = 'detail'
      // this.formData = row
    },
    deleteRow(row, index) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then((result) => {
        deleteRow({
          id: row.id
        }).then(response => {
          if (response.code === 200 || response.code === 20000) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.datas.splice(index, 1)
          } else {
            this.$message({
              type: 'info',
              message: '操作失败'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '删除取消'
        })
      })
    },
    deleteBatch() {

    }
  }
}
